package org.csu.secondhand.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.secondhand.api.entity.Comments;
import org.csu.secondhand.api.entity.Likes;
import org.csu.secondhand.api.entity.Posting;
import org.csu.secondhand.api.persistence.CommentsMapper;
import org.csu.secondhand.api.persistence.LikesMapper;
import org.csu.secondhand.api.persistence.PostingMapper;
import org.csu.secondhand.api.service.PostingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("postingService")
public class PostingServiceImpl implements PostingService {

    @Autowired
    private PostingMapper postingMapper;
    @Autowired
    private LikesMapper likesMapper;
    @Autowired
    private CommentsMapper commentsMapper;

    @Override
    public List<Posting> getAllPosting() {
        QueryWrapper<Posting> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("time");
        List<Posting> postingList = postingMapper.selectList(queryWrapper);
        return postingList;
    }

    @Override
    public List<Posting> getMyPosting(String user) {
        QueryWrapper<Posting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user);
        queryWrapper.orderByDesc("time");
        List<Posting> postingList = postingMapper.selectList(queryWrapper);
        return postingList;
    }

    @Override
    public List<Posting> getThoseILike(String user) {
        List<Likes> likesList = likesMapper.selectList(new QueryWrapper<Likes>().eq("username", user));
        List<Posting> postingList = new ArrayList<>();
        for (int i = 0; i < likesList.size(); i++) {
            Posting posting = getPostingById(likesList.get(i).getPosting());
            postingList.add(posting);
        }
        return postingList;
    }

    @Override
    public List<Posting> getThoseIComment(String user) {
        List<Comments> commentsList = commentsMapper.selectList(new QueryWrapper<Comments>().eq("username", user));
        List<Posting> postingList = new ArrayList<>();
        for (int i = 0; i < commentsList.size(); i++) {
            Posting posting = getPostingById(commentsList.get(i).getPosting());
            postingList.add(posting);
        }
        return postingList;
    }

    @Override
    public List<Posting> searchPosting(String keyword) {
        QueryWrapper<Posting> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("content", keyword.toLowerCase());
        List<Posting> postingList = postingMapper.selectList(queryWrapper);
        return postingList;
    }

    @Override
    public Posting insertPosting(Posting posting) {
        postingMapper.insert(posting);
        return posting;
    }

    @Override
    public Posting getPostingById(long id) {
        Posting posting = postingMapper.selectById(id);
        return posting;
    }

    @Override
    public Posting deletePosting(long id) {
        Posting posting = getPostingById(id);
        postingMapper.deleteById(id);
        return posting;
    }

    @Override
    public Posting editPosting(Posting posting) {
        postingMapper.updateById(posting);
        return posting;
    }

    @Override
    public void likesPlus(long id) {
        Posting posting = getPostingById(id);
        posting.setLikes(posting.getLikes() + 1);
        postingMapper.updateById(posting);
    }

    @Override
    public void likesMinus(long id) {
        Posting posting = getPostingById(id);
        posting.setLikes(posting.getLikes() - 1);
        postingMapper.updateById(posting);
    }

    @Override
    public void commentsPlus(long id) {
        Posting posting = getPostingById(id);
        posting.setComments(posting.getComments() + 1);
        postingMapper.updateById(posting);
    }

    @Override
    public void commentsMinus(long id) {
        Posting posting = getPostingById(id);
        posting.setComments(posting.getComments() - 1);
        postingMapper.updateById(posting);
    }

}
